#include <iostream>
using namespace std;
#include <sys/types.h>
#include <unistd.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <sys/wait.h>

int main()
{
    /* 创建子进程 */
    pid_t pid = fork();
    if (pid < 0)
    {
        perror("fork error:");
        /* 进程退出  */
        _exit(-1);
    }
    else if (pid == 0)
    {
        int cnt = 5;
        while (cnt--)
        {
            cout << "l am child process:" << getpid() << endl;
            cout << "l am child, parent ID:" << getppid() << endl;

            sleep(1);
        }
        cout << "子进程结束执行" << endl;
        /* 子进程退出 */
        _exit(0);
    }
    else
    {
        cout << "l am parent process:" << getpid() << endl;
        cout << "l am parent,argument pid :" << pid << endl;

        /* 阻塞回收子进程的资源 */
        /* 子进程结束会阻塞 */
        wait(NULL);

        cout << "recycle resource completed:" << endl;
    }

    while (1)
    {
        sleep(1);
    }
    return 0;
}